﻿using LiteDB;
using Nut.utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Hander.Customization
{
    class LiteHelper
    {
        /// <summary>
        /// 
        /// </summary>
        private static string DefaultLiteDBUrl { get; set; }


        #region log4net
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        #endregion

        public static void InitLiteDB(String db)
        {
            DefaultLiteDBUrl = $"{Global.Data_subdir}{Path.DirectorySeparatorChar}{db}";
            log.Debug($"init lite db url is {DefaultLiteDBUrl}");

        }


        public static LiteDatabase GetNewInstance(string location)
        {
            if (string.IsNullOrEmpty(Global.Default_pwd))
            {
                log.Error("默认密码为空，请核实");
                return null;
            }

            String path = $"Filename={location};Password={Global.Default_pwd};";
            LiteDatabase _db = new LiteDatabase(path);
            //_db.Log.Level = Logger.FULL;
            //_db.Log.Logging += Log_Logging;

            return _db;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static LiteDatabase GetNewInstance()
        {
            

            return GetNewInstance(DefaultLiteDBUrl);

        }
        enum State
        {
            CREATE, USED, RELEASE
        }
    }
}
